package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controller.DataBaseInteactor;
import controller.FrameControl;

import model.Conference;
import model.LogIn;

/**
 * Login frame, this is where passes in most of the information.
 * 
 * @author Richard Lee
 * @version 52513
 */
public class LogInFrame extends JFrame {
	
	/**
	 * Default serial version id.
	 */
	private static final long serialVersionUID = -3549883019695517886L;
	
	/**
	 * GUI component.
	 */
	private final JLabel idLabel = new JLabel("User ID: ");
	private final JLabel pwLabel = new JLabel("Password:");
	private final JLabel conLabel = new JLabel("Conferece: ");
	private final JLabel roleLabel = new JLabel("Role: ");
	private final JPanel idPanel = new JPanel(new FlowLayout());
	private final JPanel pwPanel = new JPanel(new FlowLayout());
	private final JPanel topPanel = new JPanel(new BorderLayout());
	private final JPanel bottomPanel = new JPanel(new BorderLayout());
	private final JPanel conPanel = new JPanel(new FlowLayout());
	private final JPanel buttonPanel = new JPanel();
	private final JTextField idField = new JTextField(10);
	private final JTextField pwField = new JTextField(10);
	private final JButton loginButton = new JButton("Log In");
	private final JButton registerButton = new JButton("Register");
	private final BorderLayout layout = new BorderLayout();
	
	/**
	 * List of existing conferences name.
	 */
	private String[] conferenceString = {};
	
	/**
	 * List of role available.
	 */
	private final String[] roleString = {"Author", "Reviewer", "SubProgram Chair", "Program Chair"};
	
	/**
	 * Combo box (drop down) for roles.
	 */
	private JComboBox<String> roleList = new JComboBox<String>(roleString);
	
	/**
	 * Combo box for conferences.
	 */
	private JComboBox<String> conferenceList;
	
	/**
	 * List of conferences object.
	 */
	private List<Conference> listOfConference;
	
	/**
	 * user selected conference.
	 */
	private Conference selectedConf;
	
	/**
	 * User's role
	 */
	private String role = "";
	
	/**
	 * Database interactor.
	 */
	private final DataBaseInteactor inteactor;
	
	/**
	 * Constructor.
	 * 
	 * @param DBI Database interactor.
	 */
	public LogInFrame(DataBaseInteactor DBI) {
		super("Log In");
		inteactor = DBI;
		Conference conference = new Conference(inteactor);
		listOfConference = conference.getConferences();
		conferenceList = new JComboBox<String>(conferenceString);
		conferenceList.insertItemAt(null, 0);
		for(int i = 1; i < listOfConference.size() + 1; i++) {
			conferenceList.insertItemAt(listOfConference.get(i - 1).getName(), i);
		}
		setLayout(layout);
		buttonPanel.add(loginButton);
		buttonPanel.add(registerButton);
		idPanel.add(idLabel);
		idPanel.add(idField);
		pwPanel.add(pwLabel);
		pwPanel.add(pwField);
		conPanel.add(conLabel);
		conPanel.add(conferenceList);
		conPanel.add(roleLabel);
		conPanel.add(roleList);
		topPanel.add(idPanel, BorderLayout.PAGE_START);
		topPanel.add(pwPanel, BorderLayout.PAGE_END);
		bottomPanel.add(conPanel, BorderLayout.PAGE_START);
		bottomPanel.add(buttonPanel, BorderLayout.PAGE_END);
		add(topPanel, BorderLayout.PAGE_START);
		add(bottomPanel, BorderLayout.PAGE_END);
		pack();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * Add actions to buttons.
	 */
	private void actionButton() {
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String inputID = idField.getText();
				String inputPW = pwField.getText();
				if (conferenceList.getSelectedItem() == null) {
					selectedConf = null;
				} else {
					selectedConf = listOfConference.get(conferenceList.getSelectedIndex() - 1);
				}
				role = (String) roleList.getSelectedItem();
				LogIn login = new LogIn(inputID, inputPW, selectedConf, role, inteactor);
				if(login.login()) {
					if (selectedConf == null) {
						NewConferenceView ncv = new NewConferenceView(inputID, inputPW, inteactor);
						ncv.makeNewConferenceView();
					}
					
					FrameControl fc = new FrameControl(inputID, inputPW, selectedConf, role, inteactor);
					fc.makeConfFrame();
					dispose();
				} else {
					//when Id or PW is not matched.
					JOptionPane.showMessageDialog(null, "Invalid Login");
					pwField.setText("");
					pwField.requestFocus();
				}
			}
		});
		
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				RegisterView rv = new RegisterView(inteactor, conferenceList);
				rv.makeRegisterView();
			}
		});
	}
	


}